package com.example.eback.controller;

import com.example.eback.entity.User;
import com.example.eback.result.Result;
import com.example.eback.result.ResultFactory;
import com.example.eback.util.SendMailUtil;
import com.example.eback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Controller
public class RetrievePassword {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    SendMailUtil sendMailUtil;
    @Value("${from_email}")
    String from_email;
    @Value("${from_email_pwd}")
    String from_email_pwd;

    /**
     * 发送email进行验证码的发送，并生成code存入Redis,有效未3分钟,在此期间判断邮箱格式和是否被注册
     * @param json
     * @return
     */
    @RequestMapping("/api/retrieve")
    @ResponseBody
    public Result retrieve_sendCode(@RequestParam Map<String, Object> json) {
        String message;
        try {
            String email = (String) json.get("email");
            if (!sendMailUtil.isEmail(email)) {
                message = "邮箱格式不正确";
            } else {
                User user = userService.retrieve(email);
                if (user == null) {
                    message = "此邮箱未注册";
                } else {
                    ValueOperations<String, String> operations = redisTemplate.opsForValue();
                    if(operations.get(email)!=null){
                        message= "验证码已发送,请三分钟后重试";
                    }
                    else {
                        StringBuilder code = sendMailUtil.CreateCode();
                        operations.set(email, String.valueOf(code));
                        redisTemplate.expire(email, 3, TimeUnit.MINUTES);
                        message=sendMailUtil.sendQQEmail(from_email, from_email_pwd, email, String.valueOf(code), user.getUsername());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            message = "出现未知错误";
        }
        return ResultFactory.buildFailResult(message);
    }

    /**
     * 检查两次密码是否一致,邮箱是否和获取验证码的一致,验证码是否和redis中的一致,全通过则修改成功
     * @param json
     * @return
     * @throws Exception
     */
    @PostMapping("/SaveRetrieve")
    @ResponseBody
    public Result SaveRetrieve(@RequestParam Map<String, Object> json) throws Exception {
        String password= (String) json.get("password");
        String repassword= (String) json.get("repassword");
        String email= (String) json.get("email");
        String code= (String) json.get("code");
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String get_code= operations.get(email);
        String message="";
        int SureInformation=1;
        if(!sendMailUtil.CheckPassword(password,repassword)||password.length()<3) {
            SureInformation = 0;
            message= "两次密码不匹配或者密码过短";
        }
        if(get_code==null|| !Objects.equals(code, get_code)){
            SureInformation=0;
            message = "邮箱或验证码不正确";
        }
        if(SureInformation==1) {
            if (userService.retrivePassword(repassword,email)!=null) {
                message="修改成功";
            }
            else{
                message="修改信息的用户不存在";
            }


        }

        return ResultFactory.buildFailResult(message);
    }
}